import {
  checkLogin
} from './auth.js'
new Vue({
  el: '#app',
  data: {
    IS_LOGIN: null,
    dataList: [],
    dialogFormVisible: false,
    pagination: {
      pageSizes: [5, 10, 20, 50, 100],
      queryString: '',
      currentPage: 1,
      pageSize: 5,
      total: 0
    },
    activeName: '牛逼666',
    firstCategories: [],
    secondCategories: [],
    // 是否自动上传图片
    autoUpload: true,
    imageUrl: null,
    // 表单数据
    formData: {
      itemType: '',
      title: '',
      price: '',
      num: '',
      first: '',
      categoryId: '',
      priority: '',
      status: '',
      sellPoint: ''
    }
  },
  async created() {
    this.IS_LOGIN = await checkLogin()
    if (this.IS_LOGIN == null) {
      location.href = '/'
    }
    this.getProductList()
    await this.getFirstCategory()
    // this.formData.first = this.firstCategories[0].id
    // this.searcherSecondCategory(this.formData.first)
  },
  methods: {
    // 添加商品
    async handleAdd() {
      if (this.formData.categoryId == '' || this.formData.title == '' || this.formData.price == '' || this.formData.num == '' || this.formData.priority == '' || this.formData.status == '' || this.formData.sellPoint == '' || this.imageUrl == null) {
        this.$message.error('请填写完整信息')
        return
      }
      const res = await request.post('/product/addProduct',{
        categoryId: this.formData.categoryId,
        itemType: this.formData.itemType,
        title: this.formData.title,
        price: this.formData.price,
        num: this.formData.num,
        priority: this.formData.priority,
        status: this.formData.status,
        sellPoint: this.formData.sellPoint,
        image: this.imageUrl
      })
      this.$message.success(res.msg)
      this.dialogFormVisible = false
    },
    // 获取一级分类
    async getFirstCategory() {
      const res = await request.get('/product/getFirstCategory')
      this.firstCategories = res.data
    },
    // 获取二级分类
    async searcherSecondCategory() {
      this.formData.categoryId = ''
      const res = await request.get('/product/getSecondCategory',{
        params: {
          id: this.formData.first
        }
      })
      this.secondCategories = res.data
    },
    // 上传图片
    async uploadImg(item) {
      const file = item.file
      const formData = new FormData()
      formData.append('file', file)
      const res = await request.post('/file/upload', formData, {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      })
      console.log(res)
      this.formData.imageUrl = res.data
      this.imageUrl = res.data
    },
    // 创建商品
    handleCreate() {
      this.dialogFormVisible = true
    },
    findPage() {
      this.getProductList()
    },
    async getProductList() {
      const res = await request.get('/product/getProductList', {
        params: {
          searchKey: this.pagination.queryString,
          currentPage: this.pagination.currentPage,
          pageSize: this.pagination.pageSize
        }
      })
      this.dataList = res.data.pageList
      this.pagination.total = res.data.pageTotal
    },
    handleCurrentChange(currentPage) {
      this.pagination.currentPage = currentPage
      this.getProductList()
    },
    handleSizeChange(size) {
      this.pagination.pageSize = size
      this.getProductList()
    },
    async downProduct(item) {
      let formData = new FormData()
      formData.append('id', item.id)
      formData.append('status', item.status == 1 ? 2 : 1)
      await request.put('/product/changeStatus', formData)
      this.getProductList()
    }
  }
})